package com.zzyl.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.AddNursingLeverDto;
import com.zzyl.dto.GetPageNursingsDto;
import com.zzyl.dto.UpdateNursingLeverDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.exception.BaseException;
import com.zzyl.exception.GlobalExceptionHandler;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

import static com.zzyl.enums.BasicEnum.NURSINGLEVEL_INSERT_FAIL;
import static com.zzyl.enums.BasicEnum.NURSINGLEVEL_UPDATE_FAIL;

@Service
public class NursingLevelServiceImpl implements NursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    public NursingLevelServiceImpl(NursingLevelMapper nursingLevelMapper) {
        this.nursingLevelMapper = nursingLevelMapper;
    }

    @Override
    public List<NursingLevelVo> getAllNursingLevel() {
        return nursingLevelMapper.getAllNursingLevel();
    }

    @Override
    public PageResponse getPageNursingLevel(GetPageNursingsDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<NursingLevelVo> nursingLevelVoList = nursingLevelMapper.getPageNursingLevel(dto);
        Page<NursingLevelVo> nursingLevelVoList1 = (Page<NursingLevelVo>) nursingLevelVoList;
        PageResponse pageResponse = new PageResponse();
        pageResponse.setTotal(nursingLevelVoList1.getTotal());
        pageResponse.setRecords(nursingLevelVoList1.getResult());
        return pageResponse;
    }

    @Override
    public NursingLevelVo getNursingLevelById(Integer id) {
        return nursingLevelMapper.getNursingLevelById(id);
    }

    @Override
    public void deleteNursingLevelById(Integer id) {
        nursingLevelMapper.deleteNursingLeverById(id);
    }

    @Override
    public void insertNursingLevel(AddNursingLeverDto dto) {
        List<NursingLevelVo> nursingLevelVoList = nursingLevelMapper.getNursingLevelByName(dto.getName());
        if (nursingLevelVoList.isEmpty()) {
            NursingLevel nursingLevel = new NursingLevel();
            BeanUtils.copyProperties(dto, nursingLevel);
            nursingLevelMapper.insertNursingLever(nursingLevel);
        } else {
            throw new BaseException(NURSINGLEVEL_INSERT_FAIL);
        }
    }

    @Override
    public void updateNursingLever(UpdateNursingLeverDto dto) {
        List<NursingLevelVo> nursingLevelVoList = nursingLevelMapper.getNursingLevelByName(dto.getName());
        if (nursingLevelVoList.isEmpty()) {
            NursingLevel nursingLevel = new NursingLevel();
            BeanUtils.copyProperties(dto, nursingLevel);
            nursingLevelMapper.updateNursingLever(nursingLevel);
        } else {
            throw new BaseException(NURSINGLEVEL_UPDATE_FAIL);
        }
    }

    @Override
    public void updateNursingLevel(Integer id, Integer status) {
        nursingLevelMapper.updateNursingLevel(id, status);
    }
}
